(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:MiniCtrl
     * @description
     * # MiniCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('MiniCtrl', MiniCtrl);

    MiniCtrl.$inject = ['$scope', '$rootScope', 'dataService', 'helper', '$state', '$location'];

    function MiniCtrl($scope, $rootScope, dataService, helper, $state, $location) {

        $scope.listAction = function() {
            // $scope.page_Size = 4;
            $scope.getlist(1);
            $scope.$on('event:up-store-id', function() {
                $scope.getlist(1);
            });
        }

        $scope.getlist = function(pageNo) {
            $scope.pageNo   = pageNo?pageNo:1;
            $scope.pageSize = $scope.pageSize ? $scope.pageSize : 10;
            var query = {
                content: {
                    page_no: $scope.pageNo,
                    page_size: $scope.pageSize,
                    store_id: $rootScope.store_id,
                }
            };

            if ($scope.mini_store_id) {
                query.content = {
                    mini_store_id: $scope.mini_store_id,
                    store_id: $rootScope.store_id,
                };
            }

            dataService.ministoreGet(query).then(function(list) {
                if (list) {
                    if ($scope.mini_store_id) {
                        $scope.ministore = list.ministore_list[0];
                    } else {
                        $scope.users = list.ministore_list;
                        $scope.total_count = list.total_count;
                    }

                }
            });
        }

        $scope.infoAction = function() {
            $scope.mini_store_id = $location.search().mini_store_id;
            if ($scope.mini_store_id) {
                $scope.getlist(1);
                $scope.$on('event:up-store-id', function() {
                    $scope.getlist(1);
                });
            }
        }

        $scope.uptrust = function(id) {
            var data = { content: { mini_store_id: $scope.ministore.mini_store_id, trust: id } }
            $scope.upapi(data);
        }

        //status=1正常，2冻结
        $scope.StatusUpdate = function(status, id, type, reason) {

            helper.myConfirm('confirm operation', function() {
                var data = { content: { mini_store_id: id } }
                if (type == 'status') {
                    data.content.status = status;
                }
                if (type == 'check') {
                    data.content.check = status;
                    data.content.reason = reason;
                    if (status == 3 && !reason) {
                        helper.myAlert("请添加拒绝理由！");
                        return false;
                    }
                }
                $scope.upapi(data);
            });
        }
        $scope.upapi = function(data) {
            dataService.ministoreUpdete(data).then(
                function(r) {
                    helper.myAlert("success", null, null, function() {
                        $scope.getlist(1);
                    });
                }
            );
        }
    }
})();